<?php

/**
 * @author SHINJI
 * @copyright 2009
 */

function __autoload($class_name){
  require_once $class_name . '.php';
}

class Creator extends FuncionComun
{	
	var $conexion;
	var $tabla;
	var $tabla_name;
	var $objeto_atributos_up;
	var $objeto_atributos_mid;
	var $objeto_atributos_down;
	var $template;//variable de creacion del template
	var $template2;//variable de creacion del segundo template

	
	function Creator($table,$extends)
	{
		$this->conexion=new ConexionDB();
		
		$consulta="show columns from ".$table;
		$this->conexion->realizarConsulta($consulta);
		$tabla=$this->conexion->getResultadosConsulta();
		$this->tabla_name=ucwords($table);
		$this->tabla=$tabla;
		$tab=$tabla;
		
	

		foreach($this->tabla as $cel)
			{
				$i++;
				$this->objeto_atributos_up.="
	var $".strtolower($cel[0]).";//$i";
				
			}
			
			foreach($this->tabla as $cel)
			{
				if(!$this->objeto_atributos_mid)				
				$this->objeto_atributos_mid.="$".strtolower($cel[0])."=''";
				else
				$this->objeto_atributos_mid.=",$".strtolower($cel[0])."=''";
			
			}
			
				foreach($this->tabla as $cel)
			{
				$x++;
				$this->objeto_atributos_down.='
				$this->'.strtolower($cel[0])."=$".strtolower($cel[0]).";//$x";
				
			}
			
		
		if($extends)
		{
			foreach($extends as $ex)
			{
			$consulta="show columns from ".$ex;
		$this->conexion->realizarConsulta($consulta);
		$tabla=$this->conexion->getResultadosConsulta();
		print_r($tabla);
		
		foreach($tabla as $cel)
			{
				if(!ereg($cel[0],$this->objeto_atributos_up))
				{$i++;
				$this->objeto_atributos_up.="
	var $".strtolower($cel[0]).";//$i";
				}
			}
			
			foreach($tabla as $cel)
			{
				if(!ereg($cel[0],$this->objeto_atributos_mid))
				{if(!$this->objeto_atributos_mid)				
				$this->objeto_atributos_mid.="$".strtolower($cel[0])."=''";
				else
				$this->objeto_atributos_mid.=",$".strtolower($cel[0])."=''";
				}
			
			}
			
				foreach($tabla as $cel)
			{
				if(!ereg($cel[0],$this->objeto_atributos_down)){
				$x++;
				$this->objeto_atributos_down.='
				$this->'.strtolower($cel[0])."=$".strtolower($cel[0]).";//$x";
				}
			}
		
		}
		}
		
		//CREACION DEL TEMPLATE
		
		
		foreach($tab as $t)
		{
			
			$t[0]=strtolower($t[0]);
			
			$this->template2.="
			
			<div class='informe_campo'>
			$t[0]
			</div>
			<div class='informe_dato'>
			<?=\$$this->tabla_name['$t[0]']?>				
			</div>";
				
			
			$this->template.="
			
			";
			if(ereg('int',$t[1]))
			{
			$lang=$t[1];
			$lang=str_replace(array('int','(',')'),'',$lang);
			$this->template.="<?=\$elementos_xhtml['$t[0]']?\$elementos_xhtml['$t[0]']:(\$object->$t[0]?'$t[0]: ':'').\"<input type='text' class='number' lang='0 ->$lang ->length' name='$t[0]' value='\".(\$object->$t[0]?\$object->$t[0]:'$t[0]').\"'/><br />\"?>";
			}
			else if(ereg('varchar',$t[1]))
			{
			$lang=$t[1];
			$lang=str_replace(array('varchar','(',')'),'',$lang);
			$this->template.="<?=\$elementos_xhtml['$t[0]']?\$elementos_xhtml['$t[0]']:(\$object->$t[0]?'$t[0]: ':'').\"<input type='text' class='not_null' lang='0 ->$lang ->length' name='$t[0]' value='\".(\$object->$t[0]?\$object->$t[0]:'$t[0]').\"'/><br />\"?>";
			}
			else if(ereg('text',$t[1]))
			{
			$this->template.="<?=\$elementos_xhtml['$t[0]']?\$elementos_xhtml['$t[0]']:(\$object->$t[0]?'$t[0]: ':'').\"<textarea class='not_null' name='$t[0]'>\".(\$object->$t[0]?\$object->$t[0]:'$t[0]').\"</textarea><br />\"?>";
			}
			
			else if(ereg('date',$t[1]))
			{
			$this->template.="<?=\$elementos_xhtml['$t[0]']?\$elementos_xhtml['$t[0]']:(\$object->$t[0]?'$t[0]: ':'').\"<textarea class='not_null' name='$t[0]'>\".(\$object->$t[0]?\$object->$t[0]:'$t[0]').\"</textarea><br />\"?>";
			}
			$this->template.="
			
			";
					
		}
//	
//		$this->creadorJs();
//		$this->creadorCss();
		
//		$this->creadorTpl();
$this->creadorObj();
		//$this->creadorTransaccion();
		//$this->creadorAdmin();
		//$this->creadorTpls();
		//$this->creadorTable();	
		//$this->creadorJsTable();
//	
	}
	
	function creadorTable()
		{$file=fopen(ucwords('../templates/'.$this->tabla_name)."stable.phtml","w");
		 fwrite($file,"		 
		<div class='center'>
<div class='hoja'>
	<h1><?=\$elementos_xhtml['titulo']?></h1>
<br />
	<h1><?=\$elementos_xhtml['header1']?></h1>
	<h2><?=\$elementos_xhtml['header2']?></h2>
</div>

	<table id='$this->tabla_name' class='tablesorter'>
	<thead>
	<tr> 
				<th>NOMBRE </th>
				<th>APELLIDO PATERNO </th>
				<th>APELLIDO  MATERNO</th>
				<th>CI</th>
				<th>OCUPACION </th>
			
</tr> 
</thead> 
<tbody> 
<?=\$elementos_xhtml['rows']?>
</tbody> 
</table>	
</div>");
		 }	
		
	
	function creadorJs()
		{$file=fopen('../js/'.ucwords($this->tabla_name).".js","w");
		 fwrite($file,"jQuery(function($){ $('.$this->tabla_name').formulario(true);});");
		 }	
	
	function creadorJsTable()
		{$file=fopen('../js/'.ucwords($this->tabla_name)."table.js","w");
		 fwrite($file,"$(document).ready(function(){
		 	$('#$this->tabla_name').tablesorter();}); ");
		 }	
	
	function creadorTpl()
	{
		
				$file=fopen('../templates/'.ucwords($this->tabla_name).".phtml","w");
fwrite($file,"<div class='content'>
<div class='center'>
	<div class='hoja'>
		<form action='Transaccion$this->tabla_name.php' class='$this->tabla_name' method='post'>
		<h1><?=\$elementos_xhtml['header1']?>  $this->tabla_name</h1>
		<h2><?=\$elementos_xhtml['header2']?></h2>
			<br />
				<input name='accion' type='hidden' value='<?=\$elementos_xhtml['accion']?>' />
				$this->template
		
		
		<input type='submit' value='<?=\$elementos_xhtml['boton']?>'/>
		<input type='reset' value='Reset' onclick=\"$('.$this->tabla_name').reseteo()\" name='resetButton'/>
        <input id='preview' value='Vista Previa' type='button' onclick=\"$('.$this->tabla_name').fpreview()\" name='Preview'/>
        <input id='cpreview' value='Cancelar Vista' type='button' style='display: none;' onclick=\"$('.$this->tabla_name').cpreview()\" name='Preview'/>
		</form>
	</div>
</div>
</div>
");			
	}
	
		function creadorTpls()
	{
		
				$file=fopen('../templates/'.ucwords($this->tabla_name)."s.phtml","w");
fwrite($file,"<div class='center'>
<div class='hoja'>
	<h1><?=\$elementos_xhtml['titulo']?></h1>
<br />
	<h1><?=\$elementos_xhtml['header1']?>  $this->tabla_name</h1>
	<h2><?=\$elementos_xhtml['header2']?></h2>
	
<?
foreach(\$elementos_xhtml['rows'] as \$$this->tabla_name)
{
?>
<div class='informe'>
$this->template2
</div>
<?
}
?>
</div></div>


			<br />
				
		
		<input name='accion' type='hidden' value='<?=\$elementos_xhtml['accion']?>' />
		<input type='submit' value='<?=\$elementos_xhtml['boton']?>'/>
		<input type='reset' value='Reset' onclick=\"$('.$this->tabla_name').reseteo()\" name='resetButton'/>
        <input id='preview' value='Vista Previa' type='button' onclick=\"$('.$this->tabla_name').fpreview()\" name='Preview'/>
        <input id='cpreview' value='Cancelar Vista' type='button' style='display: none;' onclick=\"$('.$this->tabla_name').cpreview()\" name='Preview'/>
	</div>
</div>
</div>
");			
	}
	
	
	
	
	function creadorObj()
	{
		$file=fopen(ucwords($this->tabla_name).".php","w");
		fwrite($file,"<?php
 class ".ucwords($this->tabla_name)."
{
$this->objeto_atributos_up

	function ".ucwords($this->tabla_name)."(".$this->objeto_atributos_mid.")
	{
		$this->objeto_atributos_down
	}
}
?>");
	}
	
	function creadorTransaccion()
	{
		$file=fopen("Transaccion".ucwords($this->tabla_name).".php","w");
		fwrite($file,"<?php
		session_start();
define('TEMPLATES', '../templates/');
define('JS','../js/');
define('CSS','../css/');
define('OBJETOS','../objetos/');
define('NEGOCIOS','../negocio/');
define('EXTENSION','.phtml');
function __autoload(\$class_name){
  require_once \$class_name . '.php';
}
 
Class Transaccion".ucwords($this->tabla_name)." {
	
	var \$AdminObj;
	var \$generador_xhtml;
	var \$vars;
	
	function Transaccion".ucwords($this->tabla_name)."(\$array_vars){
		\$this->AdminObj=new Admin".ucwords($this->tabla_name)."();
		\$this->vars=\$array_vars;
		\$this->generador_xhtml=new GeneradorXHTML();
	}

	function transNew".ucwords($this->tabla_name)."(){
		\$".ucwords($this->tabla_name)."=\$this->AdminObj->getNewObject(\"".ucwords($this->tabla_name)."\",array(".$this->tabla_name."));

		
		\$this->generador_xhtml->mostrarTpl(\$".ucwords($this->tabla_name).",array(\"tpl\"=>\"".ucwords($this->tabla_name)."\" ".$this->selectsSend." ,\"boton\"=>\"Guardar Datos\",\"accion\"=>\"transSet".ucwords($this->tabla_name)."\",\"css\"=>\"".$this->tabla_name."\",\"js\"=>\"".$this->tabla_name."\"));
	}
	
	function transSet".ucwords($this->tabla_name)."(){
		\$this->vars['id_".strtolower($this->tabla_name)."']=\$this->AdminObj->getNewID(\"".$this->tabla_name."\",\"id_".$this->tabla_name."\");
		\$".ucwords($this->tabla_name)."=\$this->AdminObj->getNewObject(\"".ucwords($this->tabla_name)."\",\$this->vars);
		\$this->AdminObj->set".ucwords($this->tabla_name)."(\$".ucwords($this->tabla_name).");
		\$this->AdminObj->redireccionar(\"Transaccion".ucwords($this->tabla_name).".php\");
	}
	
	function transGet".ucwords($this->tabla_name)."(){
		\$".ucwords($this->tabla_name)."=\$this->AdminObj->get".ucwords($this->tabla_name)."(\$this->vars['id_".strtolower($this->tabla_name)."']);
		
		".$this->selects."
		
		\$array_elementos=array(\"tpl\"=>\"".ucwords($this->tabla_name)."\" ".$this->selectsSend." ,\"boton\"=>\"Guardar cambios\",\"accion\"=>\"transUpdate".ucwords($this->tabla_name)."\",\"css\"=>\"".$this->tabla_name."\",\"js\"=>\"".$this->tabla_name."\");
		\$this->generador_xhtml->mostrarTpl(\$".ucwords($this->tabla_name).",\$array_elementos);
	}
	
	function transUpdate".ucwords($this->tabla_name)."(){
		\$".ucwords($this->tabla_name)."=\$this->AdminObj->getNewObject(\"".ucwords($this->tabla_name)."\",\$this->vars);
		\$this->AdminObj->update".ucwords($this->tabla_name)."(\$".ucwords($this->tabla_name).");
		\$this->AdminObj->redireccionar(\"Transaccion".ucwords($this->tabla_name).".php\");	
	}
	
	function transDelete".ucwords($this->tabla_name)."(){
		\$this->AdminObj->delete".ucwords($this->tabla_name)."(\$this->AdminObj->get".ucwords($this->tabla_name)."(\$this->vars['id_".strtolower($this->tabla_name)."']));
		//\$this->AdminObj->redireccionar(\"Transaccion".ucwords($this->tabla_name).".php\");
	}
	function transGet".ucwords($this->tabla_name)."s()
	{
		\$rows=\$this->generador_xhtml->generarRowsTable(\$this->AdminObj->get".ucwords($this->tabla_name)."s(),\"checkbox\",false,0);
		\$this->generador_xhtml->mostrarTpl(\"\",array(\"tpl\"=>\"".ucwords($this->tabla_name)."stable\",\"rows\"=>\$rows,\"js\"=>\"".ucwords($this->tabla_name)."stable.js\"));
	}		
}

if(\$_GET){
	\$vars_trans=array_slice(\$_GET,1);
//antes no olvidar poner una validador para que nadie pueda ejecutar las funciones
	call_user_func(array(new Transaccion".ucwords($this->tabla_name)."(\$vars_trans),current(\$_GET)?current(\$_GET):\"transGet".ucwords($this->tabla_name)."s\"));
}
else if(\$_POST){
		\$vars_trans=array_slice(\$_POST,1);
		call_user_func(array(new Transaccion".ucwords($this->tabla_name)."(\$vars_trans),current(\$_POST)?current(\$_POST):\"transGet".ucwords($this->tabla_name)."s\"));
		}
	else	call_user_func(array(new Transaccion".ucwords($this->tabla_name)."(array()),\"transGet".ucwords($this->tabla_name)."s\"));

?>");


}	function creadorAdmin()
	{
		$file=fopen("Admin".ucwords($this->tabla_name).".php","w");
		fwrite($file,"<?php

class Admin".ucwords($this->tabla_name)." extends FuncionComun{
	
	var \$conexion;
	
	function Admin".ucwords($this->tabla_name)."(){
		\$this->conexion=new ConexionDB();
	}
	
	function set".ucwords($this->tabla_name)."(".ucwords($this->tabla_name)." \$".ucwords($this->tabla_name)."){
		\$this->setObjectDB(\$".ucwords($this->tabla_name).",\"".$this->tabla_name."\");
	}
	
	function get".ucwords($this->tabla_name)."s(){
		\$consulta=\"SELECT * FROM ".$this->tabla_name." ORDER BY id_".$this->tabla_name."\";
		\$this->conexion->realizarConsulta(\$consulta);
		return \$this->conexion->getResultadosConsulta();
	}
	
	function get".ucwords($this->tabla_name)."(\$id){
		\$consulta=\"SELECT * FROM ".$this->tabla_name." WHERE id_".$this->tabla_name."=\".\$id;
		\$this->conexion->realizarConsulta(\$consulta);
		\$res=\$this->conexion->getResultadosConsultaIdentify();
		return \$this->getNewObject(\"".ucwords($this->tabla_name)."\",\$res[0]);
	}
	function update".ucwords($this->tabla_name)."(".ucwords($this->tabla_name)." \$".ucwords($this->tabla_name)."){
		\$this->updateObject(\$".ucwords($this->tabla_name).",\"".$this->tabla_name."\");
	}
	function delete".ucwords($this->tabla_name)."(".ucwords($this->tabla_name)." \$".ucwords($this->tabla_name)."){
			\$this->deleteObject(\$".ucwords($this->tabla_name).",\"".$this->tabla_name."\");
	}
}
?>");
	}
		
}
new Creator("Deposito",array());



?>
